/*
 * @Description: 文件查看列表
 * @Version: V1.0.0
 * @Author: 黄鑫
 * @Date: 2025-05-15
 * @LastEditors: 黄鑫
 * @LastEditTime: 2025-05-26
 * @FilePath: FileListView.dart
 * @Remarks: 
 */
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/route_manager.dart';
import 'package:hx_base/pages/filelistview/FileWebViewPage.dart';
import 'package:hx_base/utils/HXUtils.dart';

class FileListView<T> extends StatelessWidget {
  /// 图片数组、T是自定义model 必填
  final List<T> list;

  /// 外部决定自定义model的文件url 必填
  final String Function(T item) getFileUrl;

  // 外部获取文件类型
  final String Function(T item) getFileType;

  // 外部获取文件名字
  final String Function(T item) getFileName;

  /// 组件外边距
  final EdgeInsetsGeometry? margin;

  /// 组件内边距
  final EdgeInsetsGeometry? padding;

  /// 组件样式
  final BoxDecoration? decoration;

  ///文件item样式
  final BoxDecoration? itemDecoration;
  // 文件 item样式
  final EdgeInsetsGeometry? itemMargin;
  // 文件item 样式
  final EdgeInsetsGeometry? itemPadding;

  const FileListView({
    super.key,
    required this.list,
    required this.getFileUrl,
    required this.getFileType,
    required this.getFileName,
    this.margin,
    this.padding,
    this.decoration,
    this.itemDecoration,
    this.itemMargin,
    this.itemPadding,
  });

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: margin,
      padding: padding,
      decoration: decoration,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: list.map((toElement) {
          final fileUrl = getFileUrl(toElement);
          final fileType = getFileType(toElement);
          final fileName = getFileName(toElement);
          return Container(
            decoration: itemDecoration,
            padding: itemPadding ??
                EdgeInsets.symmetric(horizontal: 10.w, vertical: 2.h),
            margin: itemMargin,
            child: Row(
              children: [
                HXUtils.getImageIcon(fileType),
                Expanded(
                  child: Container(
                    margin: EdgeInsets.only(left: 10.w, right: 10),
                    child: Text(fileName),
                  ),
                ),
                IconButton(
                  onPressed: () {
                    Get.to(() => FileWebViewPage(fileUrl));
                  },
                  icon: Icon(
                    Icons.visibility,
                    color: Color(0xFF496BFA),
                  ),
                  style: ButtonStyle(
                    minimumSize: WidgetStatePropertyAll(Size(0, 0)),
                    tapTargetSize: MaterialTapTargetSize.shrinkWrap,
                    padding: WidgetStatePropertyAll(
                        EdgeInsets.symmetric(horizontal: 10.w, vertical: 3.h)),
                  ),
                ),
              ],
            ),
          );
        }).toList(),
      ),
    );
  }
}
